package zgrjb.snbq.teach_platform.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import zgrjb.snbq.teach_platform.component.MessageWebSocketHandler;
import zgrjb.snbq.teach_platform.component.NotificationWebSocketHandler;

/**
 * WebSocket配置类
 * 
 * @author system
 * @since 2024-12-28
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册消息WebSocket处理器
        registry.addHandler(new MessageWebSocketHandler(), "/ws/message")
                .setAllowedOrigins("*")
                .withSockJS();

        // 注册通知WebSocket处理器
        registry.addHandler(new NotificationWebSocketHandler(), "/ws/notification")
                .setAllowedOrigins("*")
                .withSockJS();
    }

    /**
     * ServerEndpointExporter作用
     * 这个Bean会自动注册使用@ServerEndpoint注解声明的websocket endpoint
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
} 